package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.IEmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * 员工控制层
 *
 * @author panda
 * @since 2022/12/25 2:18
 */
@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request) {
        log.info("登录员工姓名:{}", employee.getName());

        //在数据库中查询用户名
        LambdaQueryWrapper<Employee> queryWapper = new LambdaQueryWrapper<>();
        queryWapper.eq(Employee::getUsername, employee.getUsername());
        Employee dbEmployee = employeeService.getOne(queryWapper);
        if (dbEmployee == null) {
            return R.error("登陆失败");
        }
        //比对密码
        //密码采用md5加密
        String dbEmployeePassword = dbEmployee.getPassword();
        String formPassword = employee.getPassword();
        String md5Password = DigestUtils.md5DigestAsHex(formPassword.getBytes());
        if (!md5Password.equals(dbEmployeePassword)) {
            return R.error("登陆失败");
        }
        //判断员工状态是否禁用
        if (dbEmployee.getStatus() == 0) {
            return R.error("登陆失败");
        }

        //将结果存到session中保存
        request.getSession().setAttribute("employee", dbEmployee.getId());
        //响应到前端
        return R.success(dbEmployee);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        //使session失效
        // request.getSession().removeAttribute("employee");
        request.getSession().invalidate();
        return R.success("登出成功");
    }

    @PostMapping
    public R<String> addEmployee(@RequestBody Employee employee, HttpServletRequest request) {
        log.info("正在新增员工:{}", employee);
        //设置员工的初始密码要进行加密
        String md5Password = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(md5Password);
        //简易设置创建时间和修改时间
        //employee.setCreateTime(LocalDateTime.now());
        //employee.setUpdateTime(LocalDateTime.now());
        ////设置创建人和创建时间
        //long people = (long) request.getSession().getAttribute("employee");
        //employee.setCreateUser(people);
        //employee.setUpdateUser(people);

        //调用service保存
        //在mp插入数据时,会去看看employee实体类是否有公共字段填充字段
        employeeService.save(employee);
        return R.success("新增成功");
    }

    @GetMapping("/page")
    public R<Page> findByPage(Integer page, Integer pageSize, String name) {
        log.info("访问分页查询");
        // 1.构造分页构造器
        Page<Employee> pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        employeeService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

    @PutMapping
    public R<String> updateEmployeeStatus(@RequestBody Employee employee, HttpServletRequest request) {
        log.info("进入员工禁用阶段");
        //long eid = (long) request.getSession().getAttribute("employee");
        //employee.setUpdateTime(LocalDateTime.now());
        //employee.setUpdateUser(eid);
        employeeService.updateById(employee);
        return R.success("员工状态修改成功");

    }

    /**
     * 点击编辑,实际上是查询employee通过id
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> findById(@PathVariable long id) {
        log.info("根据id查询员工信息...");
        Employee employeeById = employeeService.getById(id);
        if (employeeById != null) {
            return R.success(employeeById);
        }
        return R.error("没有查询到员工信息");
    }
}